﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rage.Base.Abilities;
using System.Xml.Serialization;

namespace Rage.Juggernaut
{
    [GenerateSerializer]
    public class AbilityRotationList : List<AbilityRotation>
    {
        public AbilityRotationList() : base() { }
        public AbilityRotationList(IEnumerable<AbilityRotation> collection) : base(collection) { }
    }
    [GenerateSerializer]
    public class StringList : List<String>
    {
        public StringList() : base() { }
        public StringList(IEnumerable<String> collection) : base(collection) { }
    }
    public class AbilityRotation
    {
        public string Name = "Dummy";
        public string Filler = "Assault";
        [XmlIgnore]
        public Type FillerAsType {
            get {
                Type t = Type.GetType("Rage.Base.Abilities.sith_warrior." + Filler + ",Rage.Base.WPF");
                return t;
            }
        }
        [XmlIgnore]
        public List<Type> ClassAbilityPriority {
            get { return _classAbilityPriority; }
            set { _classAbilityPriority = value; }
        }
        private List<Type> _classAbilityPriority = new List<Type>();
        [XmlElement("ClassAbilityPriority")]
        public String _serializable_ClassAbilityPriority
        {
            get
            {
                String retVal = "";
                foreach (Type t in _classAbilityPriority) { retVal += t.Name + "|"; }
                return retVal.TrimEnd('|');
            }
            set
            {
                String fromValue = value;
                _classAbilityPriority.Clear();
                if (string.IsNullOrEmpty(fromValue)) { return; }
                foreach(string v in fromValue.Split('|'))
                {
                    Type t = Type.GetType("Rage.Base.Abilities.sith_warrior." + v + ",Rage.Base.WPF");
                    _classAbilityPriority.Add(t);
                }
            }
        }

        public AbilityRotation() { } // here for XML Deserialization
        public AbilityRotation(string name, string filler, string execute, params Type[] priorities)
        {
            Name = name;
            Filler = filler;
            ClassAbilityPriority = new List<Type>(priorities);
        }

        public string GetError()
        {
            //bool foundCurse = false;
            foreach (Type classability in ClassAbilityPriority)
            {
                if (!HelperFunction.ALL_CLASSABILITIES_TYPES.Contains(classability))
                {
                    return classability + " can no longer be prioritized.";
                }

                /* A REASON WHY YOUR ROTATION SUCKS AND SHOULD BE FIXED
                if (spell.StartsWith("Bane"))
                {
                    if (foundCurse)
                    {
                        return "You may only include one bane.";
                    }
                    foundCurse = true;
                }*/
            }

            /* OTHER REASONS YOUR ROTATION SUCKS AND SHOULD BE FIXED
            int immo = SpellPriority.IndexOf("Immolate");
            int conf = SpellPriority.IndexOf("Conflagrate");
            if (conf >= 0 && conf < immo)
            {
                return "Conflagrate may only appear after Immolate.";
            }

            if (immo >= 0 && SpellPriority.Contains("Unstable Affliction"))
            {
                return "Unstable Affliction and Immolate don't mix.";
            }*/

            return null;
        }

        /// <summary>
        /// Gets a modified version of the user's spell priorities, for internal purposes.
        /// </summary>
        /// <param name="spellPriority"></param>
        /// <returns></returns>
        public List<Type> GetPrioritiesForCalcs(JuggernautTalents talents, bool execute)
        {
            List<Type> forCalcs = new List<Type>(ClassAbilityPriority);
            //if (talents.Backdraft > 0 && !SpellPriority.Contains("Incinerate (Under Backdraft)")) {
            //    forCalcs.Insert(forCalcs.Count, "Incinerate (Under Backdraft)");
            //}
            /*if (!execute
                && Filler.Equals("Shadow Bolt")
                && !forCalcs.Contains("Shadow Bolt (Instant)")
                && ShadowBolt_Instant.IsCastable(talents, forCalcs))
            {
                forCalcs.Insert(forCalcs.Count, "Shadow Bolt (Instant)");
            }*/
            return forCalcs;
        }

        public bool Contains(Type classability) { return ClassAbilityPriority.Contains(classability) || Filler.Equals(classability.Name); }
    }
}
